<template>
    <div class="container">
        <div class="box">
            <div class="title-box">
                <div class="txt-box cl">
                    <div class="logo-icon left"><img src="../../assets/images/login/icon-logo.png" width="100%" height="100%" alt="icon" /></div>
                    <div class="title left">AllSale·运营系统</div>
                </div>
            </div>
            <div class="img-login-box cl">
                <div class="img-box left">
                    <img src="../../assets/images/login/pic1.png" width="100%" height="100%" alt="">
                </div>
                <div class="login-box left">
                    <div class="from-item">
                        <i class="input-icon icon-user"></i>
                        <input class="ws-input" v-model="loginForm.username" @keypress="inpute" autofocus="autofocus" placeholder="请输入用户名称" />
                    </div>
                    <div class="from-item">
                        <i class="input-icon icon-password"></i>
                        <input class="ws-input" v-model="loginForm.password" @keypress="inpute" type="password" placeholder="请输入登录密码" />
                    </div>
                    <div class="from-item">
                        <button class="ws-login-btn" @click="login" :disabled="flag" :class="{ btnDisabled:flag }">登录</button>
                    </div>
                    <div class="from-item cl">
                        <div v-show="vad.isShow" class="error-box left">
                            <i class="ivu-icon ivu-icon-information-circled" style="padding-right: 5px"></i>
                            <span v-text="vad.message"></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="news-box cl" v-show="newFlag">
                <div class="news-txt left">
                    通过设置icon属性在Button内嵌入一个Icon，或者直接在Button内使用Icon组件。使用Button的icon属性，图标位置将在最左边，如果需要自定义位置，需使用Icon组件。
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import Util from "../../common/util.js";
import $ from 'jquery';
import JsEncrypt from 'jsencrypt';
export default {
    name: "loginApp",
    components: {},
    data() {
        return {
            newsFlag: false,
            visible: false,
            query: '',
            loginForm: {
                username: '',
                password: ''
            },
            vad: {
                isShow: false,
                message: ''
            },
            flag: false
        };
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            $(function() {
                var windowHeight = $(window).height(),
                    boxHeight = $('.box').height();
                if (windowHeight > boxHeight) {
                    $('.box').css("marginTop", (windowHeight - boxHeight) / 2 + "px");
                } else {
                    $('.box').css("margin", "30px auto");
                }

            })
            $(window).resize(function() {
                var windowHeight = $(window).height(),
                    boxHeight = $('.box').height();
                if (windowHeight > boxHeight) {
                    $('.box').css("marginTop", (windowHeight - boxHeight) / 2 + "px");
                } else {
                    $('.box').css("margin", "30px auto");
                }

            });
        },
        /*文本框输入事件*/
        inpute(event) {
            let keyCode = event.keyCode;
            if (32 === keyCode) {
                event.preventDefault();
                return;
            }
            if (13 === keyCode) {
                this.login();
                return;
            }
            if (this.vad.isShow) {
                this.vad.isShow = false;
            }
        },
        /**
         * 本地校验服务
         */
        verification() {
            this.loginForm.username = this.loginForm.username.trim();
            this.loginForm.password = this.loginForm.password.trim();
            if (Util.vad.isBlank(this.loginForm.username) || Util.vad.isBlank(this.loginForm.password)) {
                this.showError('用户名和密码不能为空');
                return false;
            }
            return true;
        },
        /**
         * 显示错误信息
         */
        showError(message_) {
            this.vad.isShow = true;
            this.vad.message = message_;
        },
        /**
         * 登录接口
         */
        login() {
            if (!this.verification()) {
                return;
            }
			const _this = this;
			// _this.loginSubmit();
			// _this.encodePassword();
			//请求加密KEY
			
			this.axios.get('/woauth/pubkey?username=' + this.loginForm.username + "&type=1").then(function(response) {
				let resData = response.data;
				_this.flag = false;
				if (0 === resData.meta.code) {
					_this.loginSubmit(resData.data);
				} else {
					_this.showError('系统异常登录失败');
				}
			}, function() {
				_this.flag = false;
			});
        },
		encodePassword(pubKey, pwd) {
			let jse = new JSEncrypt();
			let publicKey = pubKey;
			jse.setPublicKey(publicKey);
			let encryptData = jse.encrypt(pwd);
			return encryptData;
		},
		loginSubmit(pubKey) {
			let obj = this;
			obj.flag = true;
			let enpwd = this.encodePassword(pubKey, this.loginForm.password);
			const postData = {
				type: 1, //运营系统
				username: this.loginForm.username,
				password: enpwd,
				grantType: 'password',
				client: 'web'
			};
			this.axios.post('/woauth/token', postData).then(function(response) {
				let resData = response.data;
				obj.flag = false;
				if (0 === resData.meta.code) {
					//登录成功
					let tokenData = resData.data;
					localStorage.clear();
					localStorage.setItem(Util.tokenKey(), JSON.stringify(tokenData));
					window.location.href = Util.homePath();
				} else {
					let errorMessage = resData.meta.message;
					obj.showError(errorMessage);
				}
			}, function() {
				obj.flag = false;
			});
		}
    }
};
</script>
<style scoped>
.btnDisabled {
    cursor: not-allowed!important;
    background: #ccc!important;
}

.cl:after,
.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

.cl,
.clearfix {
    zoom: 1;
}

.left {
    float: left;
}

.right {
    float: right;
}

.container .box {
    height: 624px;
    width: 948px;
    background: #fff;
    margin: 100px auto 0px;
}

.container .box .title-box {
    height: 104px;
    line-height: 104px;
    width: 856px;
    margin: 0 auto;
    border-bottom: 1px solid #f2f2f2;
}

.container .box .title-box .txt-box .logo-icon {
    margin-left: 240px;
}

.container .box .title-box .txt-box .logo-icon img {
    width: 55px;
    height: 55px;
    margin-top: 24px;
}

.container .box .title-box .txt-box .title {
    font-family: "Microsoft Yahei", "微软雅黑";
    margin-left: 15px;
    font-style: normal;
    font-size: 33px;
    color: #333;
}

.container .box .img-login-box {
    width: 856px;
    margin: 26px auto 0;
}

.container .box .img-login-box .img-box img {
    width: 428px;
    height: 408px;
}

.container .box .img-login-box .login-box {
    margin-left: 46px;
    width: 364px;
}

.container .box .img-login-box .login-box .from-item {
    position: relative;
}

.container .box .img-login-box .login-box div.from-item:first-child {
    margin-top: 52px;
}

.container .box .img-login-box .login-box div.from-item:last-child {
    margin-top: 18px;
}

.container .box .img-login-box .login-box div.from-item+div {
    margin-top: 34px;
}

.container .box .img-login-box .login-box .from-item .input-icon {
    position: absolute;
    top: 10px;
    display: inline-block;
    width: 40px;
    height: 30px;
    z-index: 1;
    border-right: 1px solid #e8e8e8;
    line-height: 30px;
}

.container .box .img-login-box .login-box .from-item .icon-user {
    background: url(../../assets/images/login/icon-user.png) no-repeat center;
    background-size: 30px 30px;
}

.container .box .img-login-box .login-box .from-item .icon-password {
    background: url(../../assets/images/login/icon-password.png) no-repeat center;
    background-size: 24px 29px;
}

.container .box .img-login-box .login-box .from-item input.ws-input {
    width: 100%;
    height: 50px;
    border: 1px solid #e8e8e8;
    line-height: 50px;
    font-size: 20px;
    color: #666;
    padding-left: 50px;
}

.container .box .img-login-box .login-box .from-item input.ws-input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px white inset;
}

.container .box .img-login-box .login-box .from-item .input-merchant {
    cursor: pointer;
    padding-right: 50px;
}

.container .box .img-login-box .login-box .from-item .ws-login-btn {
    width: 100%;
    height: 50px;
    background: #ed9d2c;
    border: none;
    font-size: 26px;
    color: #fff;
    cursor: pointer;
}

.container .box .img-login-box .login-box .from-item .error-box {
    height: 30px;
    line-height: 30px;
    color: #FFA847;
    font-size: 16px;
}


.container .box .news-box {
    width: 856px;
    margin: 18px auto 0;
}

.container .box .news-box .news-txt {
    width: 428px;
    max-height: 60px;
    font-size: 16px;
    line-height: 28px;
    color: #666;
    overflow: hidden;
}
</style>